/*
 * Copyright 2013-2015 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.xdtech.parking.protocol.vo;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.xdtech.parking.model.ParkingDevice;
import com.xdtech.parking.protocol.util.ParkRedis;

/**
 * 
 * @author max.zheng
 * @create 2015-6-20下午7:50:57
 * @since 1.0
 * @see
 */
public class DeviceInfo implements Serializable{
	private static final long serialVersionUID = 1L;
	private Long id;
	private String ukey;
	private String aid;
	private String gwid;
	private String ukid;
	private String tag;
	private Integer onoff;
	private Integer kwh;
	private Integer car;
	private String set;
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	public String getUkey() {
		return ukey;
	}
	public void setUkey(String ukey) {
		this.ukey = ukey;
	}
	public String getAid() {
		return aid;
	}
	public void setAid(String aid) {
		this.aid = aid;
	}
	public String getGwid() {
		return gwid;
	}
	public void setGwid(String gwid) {
		this.gwid = gwid;
	}
	public String getUkid() {
		return ukid;
	}
	public void setUkid(String ukid) {
		this.ukid = ukid;
	}
	public String getTag() {
		return tag;
	}
	public void setTag(String tag) {
		this.tag = tag;
	}
	
	
	public Integer getOnoff() {
		return onoff;
	}
	public void setOnoff(Integer onoff) {
		this.onoff = onoff;
	}
	public Integer getKwh() {
		return kwh;
	}
	public void setKwh(Integer kwh) {
		this.kwh = kwh;
	}
	public Integer getCar() {
		return car;
	}
	public void setCar(Integer car) {
		this.car = car;
	}
	public String getSet() {
		return set;
	}
	public void setSet(String set) {
		this.set = set;
	}
	/**
	 * 根据model构建vo
	 * 
	 * @author max
	 * @return
	 */
	public static DeviceInfo createItem(ParkingDevice parkingDevice) {
		DeviceInfo deviceInfo = new DeviceInfo();
		deviceInfo.setId(parkingDevice.getId());
		//区域编码
		deviceInfo.setAid(parkingDevice.getAreaCode());
		deviceInfo.setCar(parkingDevice.getHasCar());
		deviceInfo.setGwid(parkingDevice.getGwid());
		deviceInfo.setKwh(parkingDevice.getKwh());
		deviceInfo.setOnoff(parkingDevice.getStatus());
		deviceInfo.setTag(parkingDevice.getTag());
		deviceInfo.setUkey(parkingDevice.getUkey());
		//设备编码
		deviceInfo.setUkid(parkingDevice.getDeviceCode());
		//自定义属性设置填充
		deviceInfo.setSet(parkingDevice.getSetVal());
		return deviceInfo;
	}
	
	/**
	 * 
	 * @author max.zheng
	 * @create 2015-6-20下午9:18:26
	 * @modified by
	 * @return
	 */
	public static String getReturnStatus(List<DeviceInfo> deviceInfos) {
//		[{"tag":"ulock","aid":"a1","gwid":"f0001","ukid":"123456","set":”1”}]
		JSONArray json = new JSONArray();
		for (DeviceInfo deviceInfo : deviceInfos) {
			String setValue = ParkRedis.getDeviceSetValue(deviceInfo.getUkey());
			if (StringUtils.isNotBlank(setValue)) {
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("tag", deviceInfo.getTag());
				jsonObject.put("aid", deviceInfo.getAid());
				jsonObject.put("gwid", deviceInfo.getGwid());
				jsonObject.put("ukid", deviceInfo.getUkid());
				jsonObject.put("set", deviceInfo.getSet());
				json.add(jsonObject);
			}
		}
		
		return json.toString();
	}
}
